<?

require_once ( 'Gpc/User.php' );
require_once ( 'Zend/Session.php' );

abstract class Gpc_User_Adapter_Abstract extends Gpc_User {
   
   protected $username;
   protected $password;
   protected $tableName;
   protected $identityColumn;
   protected $credentialColumn;
   protected $forumPath;
   protected $cookieName;
   protected $tablePrefix;
   protected $identity;

   abstract public function loadUserSession();

   public function __construct() {
      
      $this->forumPath = Zend_Registry::get( 'site_config' )->forum->path;
      $this->tablePrefix = Zend_Registry::get( 'site_config' )->forum->{'table.prefix'};
      $this->identity = new stdClass( );
   }

   /**
    * Check for authorization against specific tasks
    * -- overwrite this for forum specific checks like member groups, etc
    *
    * @param string $authCd  Auth code to check against.
    */
   public function hasAuth( $authCd ) {
      
      switch ( $authCd ) {
         case 'admin' :
            // return true if they are an "admin"
            break;
         case 'raid_leader' :
            // return true if they are raid leader -- able to add raids
            break;
         case 'add_toons' :
            // return true if they are able to add toons
            break;
         default :
            return true;
      }
   }

   protected function _setUsername( $username ) {
      $this->username = $username;
   }

   protected function _setPassword( $password ) {
      $this->password = $password;
   }

   public function getPassword() {
      return $this->password;
   }

   public function getUsername() {
      return $this->username;
   }

   public function getTableName() {
      return $this->tableName;
   }

   public function getIdentityColumn() {
      return $this->identityColumn;
   }

   public function getCredentialColumn() {
      return $this->credentialColumn;
   }

   public function logout() {
      Zend_Auth::getInstance()->clearIdentity();
      Zend_Session::expireSessionCookie();
   }

   public function getIdentity() {
      return $this->identity;
   }

   protected function mapIdentity( $dbRow ) {
      
      if ( !isset( $this->identityMap ) ) {
         throw new Exception( "Identity map array not defined", -103 );
      }
      
      foreach ( $dbRow as $key => $val ) {
         if ( isset( $this->identityMap[$key] ) ) {
            $this->identity->{$this->identityMap[$key]} = $val;
         }
      }
      
      // Go ahead and store the raw data incase this class needs access to any of it
      $this->identity->rawData = $dbRow;
   }

   /**
    * Quick call to an internal method to translate the db row into data GPCR is looking for.
    * Then it saves it to the user's session.
    */
   protected function _translateIdentity( $rowObject ) {
      
      $this->mapIdentity( $rowObject );
      Zend_Auth::getInstance()->getStorage()->write( $this->getIdentity() );
   }
}
